'use strict';
/**
 * @name 需求详情页面Controller
 * @author zyf
 */
angular.module('metalabPartnerApp')
    .controller('DemandDetailCtrl', ['$scope', '$location', '$stateParams', 'MetaResources', 'MetaRemind', 'MetaSEO',
        function ($scope, $location, $stateParams, MetaResources, MetaRemind, MetaSEO) {
            var favoriteObject;
            /**
             * GET detail
             */
            $scope.waitingDetailPromise = MetaResources.demand.get({
                'demandId': $stateParams.demandId,
                '_embed': 'leader'
            }, function (data) {
                $scope.demandData = data;
                $scope.demandData.templateUrl = 'myPopoverTemplate.html';
                $scope.hrefSrc = $location.absUrl();
                getRecommendData(data);
                HasFavorited();
                MetaSEO.setSEO('迈科技-'+data.title+'-'+data.areas.join('/'),
                    data.enterpriseInfo+'此需求已经通过迈科技核实，确认为真实可靠的企业需求。需求详情：'+data.details,
                    data.areas ? data.areas.join(',') : '');
            }, function (error) {
                MetaRemind.error(error);
            }).$promise;

            /**
             * GET recommend demands
             */
            var getRecommendData = function (data) {
                if (data.title) {
                    $scope.waitingArticlesPromise = MetaResources.search.website({
                            "type": "demand",
                            "version": "online",
                            "online": "true",
                            "verified": "true",
                            "_range": "1_10",
                            "searchText": data.title
                        }, function (data) {
                            $scope.recommendData = data;
                        },
                        function (error) {
                            MetaRemind.error(error);
                        }).$promise;
                } else {
                    $scope.waitingArticlesPromise = MetaResources.demand.query({
                        _range: "1_10",
                        version: 'online',
                        online: true,
                        verified: true,
                        _sort: '-rank,-updatedAt'
                    }, function (data) {
                        $scope.recommendData = data;
                    }, function (err) {
                        MetaRemind.error(err);
                    }).$promise;
                }
            };

            /**
             * @function 是否已收藏
             */
            var HasFavorited = function () {
                MetaResources.favorite.exist({
                    'userId': '_self',
                    'targetId': $scope.demandData.id
                }, function (data) {
                    $scope.isFavorited = true;
                    favoriteObject = data;
                }, function () {
                    $scope.isFavorited = false;
                });
            };

            /**
             * @function 收藏
             */
            $scope.Favorite = function () {
                if ($scope.isFavorited && favoriteObject) {
                    MetaResources.favorite.delete({
                        'id': favoriteObject.id
                    }, function () {
                        favoriteObject = null;
                        $scope.isFavorited = false;
                        MetaRemind.success('您已经取消收藏该技术需求');
                    }, function () {
                        MetaRemind.error('对不起，取消收藏失败，请稍后再试');
                    });
                } else {
                    MetaResources.favorite.add({
                        userId: '_self',
                        'targetId': $scope.demandData.id,
                        'targetType': 'Demand'
                    }, null, function (success) {
                        favoriteObject = success;
                        $scope.isFavorited = true;
                        MetaRemind.success("收藏成功！");
                        // MetaRemind.success("收藏成功，您可以在右上角“我的->收藏的需求”中查看");
                    }, function (err) {
                        console.log(err);
                        $scope.isFavorited = false;
                        MetaRemind.error("对不起，收藏失败！请稍后再试……");
                    });
                }
            }
        }]);
